<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Carcategory as CarcategoryModel;
use app\common\model\UserCircle as UserCircleModel;
use app\common\model\Banner as BannerModel;
use app\common\model\Notice as NoticeModel;
use app\common\model\News as NewsModel;
use app\common\model\Config as ConfigModel;
use think\Db;
class Index extends Api
{
    protected $noNeedLogin = ['getProtocolInfo','getBannerList','getCategoryList','getClassList','getIndexCircle','getNewsList','information','getShow','getNewsInfo','getClassList'];
    protected $noNeedRight = ['*'];

    /**
     * 首页轮播
     * @path api/index/getBannerList
     */
    public function getBannerList()
    {
        $list = BannerModel::get_banner_list();
        $this->success('请求成功',$list);
    }

    /*
     * 首页分类
     * @path api/index/getCategoryList
     * */
    public function getCategoryList()
    {
        $type = $this->request->post("type",1);
        $position = $this->request->post("position",0);
        $list = CarcategoryModel::get_category_list($type,$position);
        $this->success("请求成功",$list);
    }

    /*
     * 平台公告
     * @path api/index/getNoticeList
     * */
    public function getNoticeList()
    {
        $user_id = $this->auth->user_id;
        $user = $this->auth->getUser();
        $add_time = $user->add_time;
        $page = $this->request->param("page",1);
        $list = NoticeModel::get_notice_list($page,$user_id,$add_time);
        $this->success("请求成功",$list);
    }

    /*
     * 公告详情
     * @path api/index/getNoticeInfo
     * */
    public function getNoticeInfo()
    {
        $id = $this->request->param("id",'');
        $user_id = $this->auth->user_id;
        if(!$id){
            $this->error("公告ID不能为空");
        }
        $info = NoticeModel::get_notice_info($id,$user_id);
        $this->success("请求成功",$info);
    }

    /*
     * 资讯分类
     * @path api/index/getClassList
     * */
    public function getClassList()
    {
        $list = NewsModel::get_class_list();
        $this->success("请求成功",$list);
    }

    /*
     * 资讯列表
     * @path api/index/getNewsList
     * */
    public function getNewsList()
    {
        $page = $this->request->param("page",1);
        $class_id = $this->request->param("class_id",'');
        if(!$class_id){
            $this->error("分类ID不能为空");
        }
        $list = NewsModel::get_news_list($page,$class_id);
        $this->success("请求成功",$list);
    }

    /*
     * 资讯详情
     * @path api/index/getNewsInfo
     * */
    public function getNewsInfo()
    {
        $news_id = $this->request->param("news_id",'');
        if(!$news_id){
            $this->error("资讯ID不能为空");
        }
        $info = NewsModel::get_news_info($news_id);
        $this->success("请求成功",$info);
    }

    /*
     * 协议规则
     * @path api/index/getProtocolInfo
     * */
    public function getProtocolInfo()
    {
        $id = $this->request->param("id",'');
        if(!$id){
            $this->error("协议ID不能为空");
        }
        $where = "id = {$id}";
        $info = Db::name("agreement")->where($where)->find();
        if($info && !empty($info["content"])){
            $info["content"] = preg_replace('/<o:p>\s*<\/o:p>/', '',  $info["content"]);
            $info["content"]=get_image_url($info["content"],protocol_name() . '://' . $_SERVER['SERVER_NAME']);
        }
        $this->success("请求成功",$info);
    }

    /*
     * 首页帖子
     * @path api/index/getIndexCircle
     * */
    public function getIndexCircle()
    {
        $page = $this->request->param("page",1);
        $cate_id = $this->request->param("cate_id",0);
        $sort_type = $this->request->param("sort_type",0);
        $province_id = $this->request->param("province_id",0);
        $city_id = $this->request->param("city_id",0);
        $area_id = $this->request->param("area_id",0);
        $title = $this->request->param("title");
        $user_id = $this->auth->user_id;
        $list = UserCircleModel::get_index_circle($page,$cate_id,$sort_type,$province_id,$city_id,$area_id,$title,$user_id);
        $this->success("请求成功",$list);
    }

    /*
     * 图文信息
     * @path api/index/information
     * */
    public function information()
    {
        $id = $this->request->param("id");
        if(!$id){
            $this->error("轮播图id不能为空");
        }
        $where = "id = {$id}";
        $field = "id,is_jump,content";
        $info = Db::name("banner")->where($where)->field($field)->find();
        if(!$info){
            $this->error("轮播图信息错误");
        }
        if($info && !empty($info["content"])){
            $info["content"]=get_image_url($info["content"],protocol_name() . '://' . $_SERVER['SERVER_NAME']);
        }
        $this->success("请求成功",$info);
    }

    /*
     * 部分内容
     * @path api/index/getShow
     * */
    public function getShow()
    {
        $params = $this->request->param();
        if(empty($params['version']) || empty($params['duan'])){
            $this->error("参数异常");
        }
        $is_show = Db::name("show")->where(["version"=>$params["version"],"duan"=>$params["duan"]])->order("id","desc")->value("is_show");
        if(!$is_show){
            $is_show = 0;
        }
        $this->success("请求成功",$is_show);
    }

}
